package com.dreamcode.caloriestracker.activities;

import com.dreamcode.caloriestracker.collectors.ProfileDAOimpl;
import com.dreamcode.caloriestracker.modelclass.CalculationDetails;
import com.dreamcode.caloriestracker.modelclass.ProfileDetails;
import com.dreamcode.caloriestracker.utils.CalorieUtils;
import com.dreamcode.caloriestracker.utils.MathBodyComp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ProfileInfoActivity extends Activity {
	private ProfileDAOimpl profileDAO = null;
	private ProfileDetails profileDetails = null;
	private TextView tvLabel = null;
	private TextView tvAge = null;
	private TextView tvSex = null;
	private TextView tvWeight = null;
	private TextView tvHeight = null;
	private TextView tvWaist = null;

	private TextView tvBmIndex = null;
	private TextView tvLbmIndex = null;
	private TextView tvIbwIndex = null;
	private TextView tvHratio = null;
	private TextView tvBfat = null;
	private TextView tvBmRate = null;
	private TextView tvSarea = null;
	private TextView tvIbwe = null;
	private Intent it = null;
	private Button backBtn = null;

	private LinearLayout lyline = null;
	private CalculationDetails calculationDetails = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.profileinfo);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initValues();
	}

	public void initView() {
		tvLabel = (TextView) findViewById(R.id.tvlabel);
		tvAge = (TextView) findViewById(R.id.tvage);
		tvSex = (TextView) findViewById(R.id.tvsex);
		tvWeight = (TextView) findViewById(R.id.tvweight);
		tvHeight = (TextView) findViewById(R.id.tvheight);
		tvWaist = (TextView) findViewById(R.id.tvwaist);
		lyline = (LinearLayout) findViewById(R.id.lyline);
		lyline.setBackgroundColor(Color.WHITE);
		tvBmIndex = (TextView) findViewById(R.id.tvbmindex);
		tvLbmIndex = (TextView) findViewById(R.id.tvlbmindex);
		tvIbwIndex = (TextView) findViewById(R.id.tvibwindex);
		tvIbwe = (TextView) findViewById(R.id.tvibwe);
		tvHratio = (TextView) findViewById(R.id.tvhratio);
		tvBfat = (TextView) findViewById(R.id.tvbfat);
		tvBmRate = (TextView) findViewById(R.id.tvbmrate);
		tvSarea = (TextView) findViewById(R.id.tvsarea);
		backBtn = (Button) findViewById(R.id.btnok);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
		initValues();
	}

	public void initValues() {
		profileDAO = ProfileDAOimpl.getInstance();
		profileDetails = profileDAO.getProfileDetails(ProfileInfoActivity.this,
				0);

		if (profileDetails != null) {
			tvLabel.setText(profileDetails.getProfileLabel());
			tvAge.setText(profileDetails.getAge());
			tvSex.setText(profileDetails.getSex());
			tvWeight.setText(profileDetails.getProfileWeight() + " "
					+ profileDetails.getWeParam());
			tvHeight.setText(profileDetails.getProfileHeight() + " "
					+ profileDetails.getHeParam());
			tvWaist.setText(profileDetails.getProfileWaist() + " "
					+ profileDetails.getWaParam());
			calculate();
		} else {
			tvLabel.setText("undefined");
			tvAge.setText("undefined");
			tvSex.setText("undefined");
			tvWeight.setText("undefined");
			tvHeight.setText("undefined");
			tvWaist.setText("undefined");

			tvBmIndex.setText("");
			tvHratio.setText("");
			tvBfat.setText("");
			tvBmRate.setText("");
			tvSarea.setText("");
			tvLbmIndex.setText("");
			tvIbwIndex.setText("");
			tvIbwe.setText("");

		}

	}

	/**
	 * 1ft = 30.48cm 1lb = 0.45kg 1inch = 2.54cm
	 * 
	 * 1kg = 2.2046pounds
	 */
	public void calculate() {
		calculationDetails = CalorieUtils.calculateProfileData(Integer
				.parseInt(profileDetails.getAge()), profileDetails.getSex()
				.equals("Male") ? true : false, Double
				.parseDouble(profileDetails.getWeParam().equals("kg")?profileDetails.getProfileWeight():String.valueOf(Math.round(Double.parseDouble(profileDetails.getProfileWeight())/2.2))), Double
				.parseDouble(profileDetails.getHeParam().equals("cm")?profileDetails.getProfileHeight():String.valueOf(Math.round(Double.parseDouble(profileDetails.getProfileHeight())*2.54))), Double
				.parseDouble(profileDetails.getWaParam().equals("cm")?profileDetails.getProfileWaist():String.valueOf(Math.round(Double.parseDouble(profileDetails.getProfileWaist())*2.54))));
		
		tvBmIndex.setText(String.valueOf(profileDetails.getWeParam().equals("kg")?calculationDetails.getBodyMassIndex():Math.round(calculationDetails.getBodyMassIndex()/2.2))
				+ " "+profileDetails.getWeParam()+" "
				+ MathBodyComp
						.GetBMITerm(calculationDetails.getBodyMassIndex()));
		tvHratio.setText(String.valueOf(calculationDetails
				.getWaistToHeightRatio())
				+ " %");
		tvBfat.setText(String.valueOf(calculationDetails.getBodyFat())
				+ " % "
				+ MathBodyComp
						.GetBodyFatTerm(
								profileDetails.getSex().equals("Male") ? true
										: false, calculationDetails
										.getBodyFat()));
		tvBmRate.setText(String.valueOf(calculationDetails
				.getBasalMetabolicRate())
				+ " kcal/day");
		tvSarea.setText(String.valueOf(calculationDetails.getSurfaceArea())
				+ " m2");
		tvLbmIndex.setText(String.valueOf(profileDetails.getWeParam().equals("kg")?calculationDetails
				.getLeanBodyMassIndex():Math.round(calculationDetails.getLeanBodyMassIndex()/2.2))
				+ " "+profileDetails.getWeParam());
		tvIbwIndex.setText((profileDetails.getWeParam().equals("kg")?calculationDetails.getRecommendedBodyWeight().get(0):Math.round(Double.parseDouble(String.valueOf(calculationDetails.getRecommendedBodyWeight().get(0)))*2.2))				
				+ " - "
				+ (profileDetails.getWeParam().equals("kg")?calculationDetails.getRecommendedBodyWeight().get(1):Math.round(Double.parseDouble(String.valueOf(calculationDetails.getRecommendedBodyWeight().get(1)))*2.2))
				+ " "+profileDetails.getWeParam());
		tvIbwe.setText(String.valueOf(profileDetails.getWeParam().equals("kg")?calculationDetails.getIdealBodyWeight():Math.round(calculationDetails.getIdealBodyWeight()*2.2))
				+ " "+profileDetails.getWeParam());

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.profilemenu, menu);
		/*menu.add(0, 0, 0, "Create Account");
		menu.add(0, 1, 0, "Edit Account");
		menu.add(0, 2, 0, "Delete Account");*/

		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.create_account:
			if (profileDetails == null) {
				it = new Intent(ProfileInfoActivity.this,
						ProfileCreationActivity.class);
				it.putExtra("edit", false);
				startActivity(it);
			} else {
				Toast.makeText(ProfileInfoActivity.this, "The profile exist",
						Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.edit_account:
			if (profileDetails != null) {
				it = new Intent(ProfileInfoActivity.this,
						ProfileCreationActivity.class);
				it.putExtra("edit", true);
				startActivity(it);
			} else {
				Toast.makeText(ProfileInfoActivity.this, "Empty profile",
						Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.delete_account:
			if (profileDetails != null) {
				Builder yesNoBuilder = new AlertDialog.Builder(
						ProfileInfoActivity.this);
				yesNoBuilder.setTitle("Profile Info").setMessage(
						"Are you sure?").setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								profileDAO.deleteProfileAccount(
										ProfileInfoActivity.this,
										profileDetails.getId());
								Toast
										.makeText(
												ProfileInfoActivity.this,
												"The profile is deleted, please cerate new profile",
												Toast.LENGTH_LONG).show();
								initValues();
							}
						}).setNegativeButton("No", null).show();
			} else {
				Toast.makeText(ProfileInfoActivity.this, "Empty profile",
						Toast.LENGTH_LONG).show();
			}
			break;
		}

		return false;
	}

}
